﻿using System;
using System.Web;
using System.Web.Services;
using System.ComponentModel;
using System.Reflection;
using System.Collections.Generic;

using SBPweb.Services.Base;
using SBPweb.Services.Crawlers.Base;

namespace SBPweb.Services.Crawlers.IWIW
{

    [WebService(
        Name = "SBPweb Crawler service - IWIW",
        Namespace = "http://services.sbpweb.hu/crawlers/IWIW",
        Description = "services.sbpweb.hu/crawlers/IWIW")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [ToolboxItem(false)]
    public class Service : ServiceBase
    {

        #region Private fields

        private Uri baseUri;

        #endregion

        #region Constructor logic

        public Service()
        {
            this.baseUri = new Uri(Properties.Settings.Default.BaseUri);
        }

        public Service(Uri baseUri)
        {
            this.baseUri = baseUri;
        }

        #endregion

        #region Property accessors

        private static string Key_Client = "Client";
        public Client CurrentClient
        {
            get
            {
                lock (Session)
                {
                    Client ret = Session[Key_Client] as Client;
                    if (ret != null)
                    {
                        return ret;
                    }
                    Session.Add(Key_Client, ret = new Client(baseUri));
                    return ret;
                }
            }
        }

        #endregion

        #region Methods

        [WebMethod(EnableSession = true)]
        public void SignIn(string userName, string password)
        {
            ServiceMethodResult result = CurrentClient.SignIn(userName, password);
            if (result.Code.Value == ServiceResultCode.Ok)
            {
                return;
            }

            throw new HttpException(500, string.Format("ErrorCode: {0}", result.Code));
        }

        [WebMethod(EnableSession = true)]
        public void SignOut()
        {
            Session.Abandon();
        }

        //[WebMethod(true)]
        //public SBPweb.Services.Crawlers.Movies.Movies.TitleRow RequestTitleById(string titleId)
        //{

        //    Uri uri = new Uri(string.Format("{0}pls/fi/films.film_page?i_film_id={1}", pBaseUri, titleId));
        //    Client client = new Client(uri);
        //    string content = client.ParseRequest(uri);
        //    return null;
        //}

        #endregion

    }

}